from bll import mengdaili
from dal import database
from config import settings
import requests
import json

if __name__ == '__main__':
    session = requests.Session()
    login_ok = mengdaili.login(session, settings.MENGDAILI['urls']['login_url'], settings.MENGDAILI['user']['username'], settings.MENGDAILI['user']['password'])
    db = database.Database.get_instance()
    if login_ok:
        sign_ok = mengdaili.sign_in(session, settings.MENGDAILI['urls']['signin_url'])
        if sign_ok:
            mengdaili.get_pay_uid(session, settings.MENGDAILI['urls']['pay_url'])
            order_id = mengdaili.get_order(session, settings.MENGDAILI['urls']['order_url'])
            ips = mengdaili.get_ips(session, order_id)
        else:
            try:
                order_id = mengdaili.get_order(session, settings.MENGDAILI['urls']['order_url'])
                ips = mengdaili.get_ips(session, order_id)
                ips = json.loads(ips)
            except:
                uid = mengdaili.get_pay_uid(session, settings.MENGDAILI['urls']['pay_url'], settings.MENGDAILI['pay_types'][1], settings.MENGDAILI['pay_dates'][1])
                mengdaili.hb_pay(session, settings.MENGDAILI['urls']['hb_py_url'], uid)
                order_id = mengdaili.get_order(session, settings.MENGDAILI['urls']['order_url'])
                mengdaili.get_ips(session, order_id)
                ips = mengdaili.get_ips(session, order_id)
                ips = json.loads(ips)
            sql = "insert into meng(ip) values (%s)"
            db.insert(sql, *ips)